package com.ly1.aicosplay.controller;

import com.ly1.aicosplay.domain.po.Character;
import com.ly1.aicosplay.service.CharacterService;
import com.ly1.aicosplay.service.ChatMemoryService;
import com.ly1.aicosplay.domain.dto.ChatWithMemoryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/characters")
@CrossOrigin
public class CharacterController {
    
    @Autowired
    private CharacterService characterService;
    
    @Autowired
    private ChatMemoryService chatMemoryService;
    
    /**
     * 搜索角色
     * @param keyword 搜索关键词
     * @return 角色列表
     */
    @GetMapping("/search")
    public List<Character> searchCharacters(@RequestParam String keyword) {
        return characterService.searchCharacters(keyword);
    }
    
    /**
     * 获取角色详情
     * @param id 角色ID
     * @return 角色详情
     */
    @GetMapping("/{id}")
    public Character getCharacter(@PathVariable String id) {
        return characterService.getCharacter(id);
    }
    
    /**
     * 文本聊天（无记忆）
     * @param id 角色ID
     * @param message 用户消息
     * @return AI回复
     */
    @PostMapping("/{id}/chat")
    public String chatWithCharacter(@PathVariable String id, @RequestBody String message) {
        return characterService.chatWithCharacter(id, message);
    }
    
    /**
     * 带记忆的文本聊天
     * @param id 角色ID
     * @param request 聊天请求（包含会话ID和消息）
     * @return AI回复
     */
    @PostMapping("/{id}/chat-with-memory")
    public String chatWithMemory(@PathVariable String id, 
                                 @RequestBody ChatWithMemoryRequest request) {
        return chatMemoryService.chatWithMemory(request.getConversationId(), id, request.getMessage());
    }
}